/*
 * @Author       : ljkt
 * @Version      : 1.0
 * @Date         : 2021-10-05 18:00:43
 * @LastEditors: ljkt
 * @LastEditTime: 2024-04-28 10:55:16
 * @Description  :
 */

#include "Dev_Include.h"
s_dev_rgb_t gDev_RGB;

#define RGB_R_G_B_Val(r, g, b) (gDrv_Pwm[e_Index_Pwm_Rgb_R].Duty = r << 2, gDrv_Pwm[e_Index_Pwm_Rgb_G].Duty = g << 2, gDrv_Pwm[e_Index_Pwm_Rgb_B].Duty = b << 2)

#define RGB_OFF() (RGB_R_G_B_Val(0, 0, 0))      //(SET_RGB_PWM(R(0), G(0), B(0)))
#define RGB_ON() (RGB_R_G_B_Val(250, 250, 250)) // (SET_RGB_PWM(R(250), G(250), B(250)))
#define RGB_G() (RGB_R_G_B_Val(0, 250, 0))      //(SET_RGB_PWM(R(0), G(250), B(0)))
#define RGB_B() (RGB_R_G_B_Val(0, 0, 250))      //(SET_RGB_PWM(R(0), G(0), B(250)))
#define RGB_Y() (RGB_R_G_B_Val(250, 250, 0))    //(SET_RGB_PWM(R(250), G(250), B(0)))
#define RGB_O() (RGB_R_G_B_Val(250, 100, 0))    //(SET_RGB_PWM(R(250), G(100), B(0)))
#define RGB_R() (RGB_R_G_B_Val(250, 0, 0))      // (SET_RGB_PWM(R(250), G(0), B(0)))

// #define RGB_OFF() (RGB_R_G_B_Val(0, 0, 0))      //(SET_RGB_PWM(R(0), G(0), B(0)))
// #define RGB_ON() (RGB_R_G_B_Val(250, 250, 250)) // (SET_RGB_PWM(R(250), G(250), B(250)))
// #define RGB_G() (RGB_R_G_B_Val(0, 250, 0))      //(SET_RGB_PWM(R(0), G(250), B(0)))
// #define RGB_B() (RGB_R_G_B_Val(0, 0, 250))      //(SET_RGB_PWM(R(0), G(0), B(250)))
// #define RGB_Y() (RGB_R_G_B_Val(100, 150, 0))    //(SET_RGB_PWM(R(250), G(250), B(0)))
// #define RGB_O() (RGB_R_G_B_Val(150, 100, 0))    //(SET_RGB_PWM(R(250), G(100), B(0)))
// #define RGB_R() (RGB_R_G_B_Val(250, 0, 0))      // (SET_RGB_PWM(R(250), G(0), B(0)))
void Dev_RGB_Init(void)
{
    my_memclr_00(&gDev_RGB, sizeof(s_dev_rgb_t));
}
void Dev_RGB_Cycle(u8_t clr)
{
    static u8_t step = 0;
    static u16_t tick;
    if (clr == REF_CLR)
    {
        step = 0;
        tick = Drv_Get_Tick_u16();
    }

    step = step & 0x03;
    if (step == 0) //
    {
        RGB_G();
    }
    else if (step == 1) //
    {
        RGB_Y();
    }

    else if (step == 2) //
    {
        RGB_O();
    }
    else if (step == 3) //
    {
        RGB_R();
    }

    if (Drv_Autoload_Ntick_u16(&tick, 1000ul))
    {
        step++;
    }
}
void Dev_RGB_TEST(void)
{
    static u16_t ljt = 0;
    LIMIT_CYCLE_ADD(ljt, 250);
    ljt++;
    RGB_R_G_B_Val(ljt, 250 - ljt, 0);
}

void Dev_RGB_Scan(void)
{
    static u8_t tick = 0, cmd = 0;

    if (Drv_Autoload_Ntick_u8(&tick, 100ul))
    {
        if (gDev_RGB.Cmd != cmd)
        {
            cmd = gDev_RGB.Cmd;
            Dev_RGB_Cycle(REF_CLR);
        }

        switch (cmd)
        {
        case e_RGB_OFF:
            RGB_OFF();
            break;
        case e_RGB_ALL:
            RGB_ON(); //
            break;
        case e_RGB_CYCLE:
            Dev_RGB_Cycle(REF_NOCLR);
            break;
        case e_RGB_GREEN:
            RGB_G();
            break;
        case e_RGB_YELLOW:
            RGB_Y();
            break;
        case e_RGB_ORANGE:
            RGB_O();
            break;
        case e_RGB_RED:
            RGB_R();
            break;

        default:
            break;
        }
#ifdef POLARITY_INVERSION
        gDrv_Pwm[e_Index_Pwm_Rgb_R].Duty = RGB_PWM_FULL - gDrv_Pwm[e_Index_Pwm_Rgb_R].Duty;
        gDrv_Pwm[e_Index_Pwm_Rgb_G].Duty = RGB_PWM_FULL - gDrv_Pwm[e_Index_Pwm_Rgb_G].Duty;
        gDrv_Pwm[e_Index_Pwm_Rgb_B].Duty = RGB_PWM_FULL - gDrv_Pwm[e_Index_Pwm_Rgb_B].Duty;
#endif
    }
}
void Dev_RGB_Level_Scan(u8_t level)
{
    static u8_t tick = 0, cmd = 0;
    u32_t data_r = 0, data_g = 0, data_b = 0;

    if (Drv_Autoload_Ntick_u8(&tick, 100ul))
    {
        if (gDev_RGB.Cmd != cmd)
        {
            cmd = gDev_RGB.Cmd;
            Dev_RGB_Cycle(REF_CLR);
        }

        switch (cmd)
        {
        case e_RGB_OFF:
            RGB_OFF();
            // Dev_RGB_TEST();
            // level = 100;

            break;
        case e_RGB_ALL:
            RGB_ON(); //
            break;
        case e_RGB_CYCLE:
            Dev_RGB_Cycle(REF_NOCLR);
            break;
        case e_RGB_GREEN:
            RGB_G();
            break;
        case e_RGB_YELLOW:
            RGB_Y();
            break;
        case e_RGB_ORANGE:
            RGB_O();
            break;
        case e_RGB_RED:
            RGB_R();
            break;

        default:
            break;
        }
        data_r = gDrv_Pwm[e_Index_Pwm_Rgb_R].Duty;
        data_g = gDrv_Pwm[e_Index_Pwm_Rgb_G].Duty;
        data_b = gDrv_Pwm[e_Index_Pwm_Rgb_B].Duty;

        gDrv_Pwm[e_Index_Pwm_Rgb_R].Duty = data_r * level / 100ul;
        gDrv_Pwm[e_Index_Pwm_Rgb_G].Duty = data_g * level / 100ul;
        gDrv_Pwm[e_Index_Pwm_Rgb_B].Duty = data_b * level / 100ul;
#ifdef POLARITY_INVERSION
        gDrv_Pwm[e_Index_Pwm_Rgb_R].Duty = RGB_PWM_FULL - gDrv_Pwm[e_Index_Pwm_Rgb_R].Duty;
        gDrv_Pwm[e_Index_Pwm_Rgb_G].Duty = RGB_PWM_FULL - gDrv_Pwm[e_Index_Pwm_Rgb_G].Duty;
        gDrv_Pwm[e_Index_Pwm_Rgb_B].Duty = RGB_PWM_FULL - gDrv_Pwm[e_Index_Pwm_Rgb_B].Duty;
#endif
    }
}